VFG.DrawRectangle=function(viewer,option){
	this.viewer=viewer;
	this.option=option;
	this.id=option.id;
	this.thing=option.thing;
	this.position=null;
	this.tip=new VFG.Tip(viewer);
	this.enity;
	this.positions=[];
	this.LEFT_CLICK_TIMES=0
	this.init();
	
}

VFG.DrawRectangle.prototype.init=function() {
	let _this=this;
	this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	this.handler.setInputAction(function (movement) {
		let cartesian3=VFG.Util.getScreenToC3 (_this.viewer, movement.position, _this.enity) 
		if(Cesium.defined(cartesian3)){
			_this.LEFT_CLICK_TIMES+=1;
			if (_this.positions.length == 0) {
	        	_this.positions.push(cartesian3.clone());
/*	        	_this.firstEntity = _this.viewer.entities.add({
        		    position: cartesian3.clone(),
        		    point: {
        				 pixelSize: 5,
        		         color:  Cesium.Color.RED,
        		         outlineColor: Cesium.Color.YELLOW,
        		         outlineWidth: 2,            
        		    },
        		});*/
	        }
			if(_this.LEFT_CLICK_TIMES>1){
				_this.positions.pop();
			}
			_this.positions.push(cartesian3);
			if(_this.LEFT_CLICK_TIMES>=2){
				if(_this.success){
					_this.success(_this.positions);
				}
				_this.destroy();
			}
		}
    },Cesium.ScreenSpaceEventType.LEFT_CLICK)
    this.handler.setInputAction(function (movement) {
    	if (_this.LEFT_CLICK_TIMES == 0){
    		_this.tip.showAt(movement.endPosition,"单击开始绘制，右键退出");
    	}else{
    		_this.tip.showAt(movement.endPosition,"绘制中，右键退出");
    	}
    	if(_this.positions.length>0){
			let cartesian3=VFG.Util.getScreenToC3 (_this.viewer,movement.endPosition, _this.enity) 
	        if(Cesium.defined(cartesian3)){
	        	_this.positions.pop();
	        	cartesian3.y += (1 + Math.random());
	            _this.positions.push(cartesian3);
/*	            if(!Cesium.defined(_this.secondEntity)){
		        	_this.secondEntity = _this.viewer.entities.add({
	        		    position:new Cesium.CallbackProperty(function () {
	        				return _this.positions[1];
	        			}, false),
	        		    point: {
	        				 pixelSize: 5,
	        		         color:  Cesium.Color.RED,
	        		         outlineColor: Cesium.Color.YELLOW,
	        		         outlineWidth: 2,            
	        		    },
	        		});
	            }*/
	        }
			
			if(!_this.entity){
	            _this.entity= _this.viewer.entities.add({
	                rectangle : {
	        	       coordinates :new Cesium.CallbackProperty(function (time) {
							return Cesium.Rectangle.fromCartesianArray(_this.positions)
					   }, false),
	        	       material : Cesium.Color.RED.withAlpha(0.4),
                       outline : true,
                       fill: true,
                       outlineWidth: 2,
                       outlineColor : Cesium.Color.YELLOW,
	        	    }
	           });
			}
		}
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	this.handler.setInputAction(function (movement) {
		_this.destroy();
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
},

VFG.DrawRectangle.prototype.addListener=function(type,callback){
	if('success'==type){
		this.success=callback;
	}
	if('error'==type){
		this.error=callback;
	}
	if('finish'==type){
		this.finish=callback;
	}
}

VFG.DrawRectangle.prototype.removeListener=function(type){
	if('success'==type){
		this.success=null;
	}
	if('error'==type){
		this.error=null;
	}
	if('finish'==type){
		this.finish=null;
	}
}

VFG.DrawRectangle.prototype.destroy = function() {
	if(this.finish){
		this.finish();
	}
    if( this.handler){
    	this.handler.destroy();
    	this.handler = null;
    };
    if(this.tip){
    	this.tip.destroy();
    }
	if(this.entity){
		//this.entity.destroy();
	}
	delete this.entity;
    delete this.viewer;
	return Cesium.destroyObject(this);
};
